package pl.edu.agh.ssm.monitor.utils;

import java.util.HashMap;

import pl.edu.agh.ssm.monitor.data.SessionDescription;
import pl.edu.agh.ssm.monitor.data.SessionNodeType;
import pl.edu.agh.ssm.monitor.data.SessionDescription.MediaDescription;
//import pl.edu.agh.ssm.monitor.data.SessionDescription.TimeDescription;

public class SessionDataUtils {

	private static HashMap<SessionNodeType, Long> times;
	static {
		times = new HashMap<SessionNodeType, Long>();
		times.put(SessionNodeType.SENDER, Long.valueOf(10000));   //10 seconds
		times.put(SessionNodeType.SERVER, Long.valueOf(120000)); //120 seconds
		times.put(SessionNodeType.MULTIPLEXER, Long.valueOf(10000));	//10 seconds;
		times.put(SessionNodeType.RECEIVER, Long.valueOf(10000));	//10 seconds;
		times.put(SessionNodeType.SENDER_AND_RECEIVER, Long.valueOf(10000));	//10 seconds;
		
	}
	
	public static SessionNodeType getSessionNodeType(HashMap<SessionNodeType, Long> stateMap) {
		if(stateMap.containsKey(SessionNodeType.SERVER))
			return SessionNodeType.SERVER;
		if(stateMap.containsKey(SessionNodeType.MULTIPLEXER))
			return SessionNodeType.MULTIPLEXER;
		if(stateMap.containsKey(SessionNodeType.SERVER) && 
				stateMap.containsKey(SessionNodeType.RECEIVER))
			return SessionNodeType.SENDER_AND_RECEIVER;
		if(stateMap.containsKey(SessionNodeType.SENDER))
			return SessionNodeType.SENDER;
		if(stateMap.containsKey(SessionNodeType.RECEIVER))
			return SessionNodeType.RECEIVER;
		return SessionNodeType.UNKNOWN;
	}
	
	public static boolean isValidTime(SessionNodeType nodeType, Long activity) {
		long timeout = times.get(nodeType).longValue();
//		System.out.println(nodeType + " Timeout: " + timeout  + ", current diff: " + (System.currentTimeMillis() - activity.longValue()));
		if(System.currentTimeMillis() - activity.longValue() > timeout)
			return false;
		return true;
	}
	
	public static void copySessionDescription(SessionDescription from, SessionDescription to) {
		to.setConnectionInfo(from.getConnectionInfo());
		to.setDescription(from.getDescription());
		to.setEmail(from.getEmail());
		to.setEncryptionKey(from.getEncryptionKey());
		to.setName(from.getName());
		to.setOwner(from.getOwner());
		to.setPhone(from.getPhone());
		to.setTimeZoneAdjustment(from.getTimeZoneAdjustment());
		to.setType(from.getType());
		to.setUri(from.getUri());
		to.setVersion(from.getVersion());
		for(String tmp: from.getBandwidth()) {
			to.addBandwidth(tmp);
		}
		for(Integer key : from.getRtpmap().keySet()) {
//			System.out.println("Copy rtpmap setting: " + key);
			to.addMap(key, from.getRtpmap().get(key));
		}
		for(MediaDescription mediaDescription : from.getMediaDescriptions()) {
			to.addMediaDescription(mediaDescription);
		}
		for(String attribute : from.getSessionAttributes()) {
			to.addSessionAttribute(attribute);
		}
//		for(TimeDescription timeDescription : from.getTimeDescriptions()) {
//			to.addTimeDescription(timeDescription);
//		}
		
	}
}
